package com.wj.springcloud.controller;

import com.wj.spingcloud.entity.CommonResult;
import com.wj.spingcloud.entity.PaymentEntity;
import com.wj.springcloud.service.Paymentservice;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: wj
 * @create: 2021/5/12 11:19
 */
@RestController
@RequestMapping("/provider")
public class PaymentController {

    @Resource
    private Paymentservice paymentservice;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/payment/create")
    public CommonResult create(@RequestBody PaymentEntity paymentEntity) {
        int result = paymentservice.create(paymentEntity);
        if (result > 0){
            return new CommonResult(200, "插入成功, serverPort:"+serverPort, result);
        } else {
            return new CommonResult(444, "插入失败");
        }
    }

    @GetMapping("/payment/get/{id}")
    public CommonResult<PaymentEntity> getPaymentById(@PathVariable("id") Long id) {
        CommonResult commonResult = new CommonResult();
        PaymentEntity paymentEntity = paymentservice.getPaymentById(id);
        if (paymentEntity != null){
            commonResult = new CommonResult(200, "查询成功, serverPort:"+serverPort, paymentEntity);
        } else {
            commonResult = new CommonResult(444, "没有该条记录");
        }
        return commonResult;
    }

    @GetMapping("/payment/lb")
    public String getPayment(){
        return serverPort;
    }

    @GetMapping("/payment/timeout")
    public String getPaymentTimeOut(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }
}